package com.ali.ttt.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author : yuxl
 * @create 2024/8/1 11:20 AM
 */
@Slf4j
@RestController
@RequestMapping("/api/thread")
@Api(tags = "线程操作")
@AllArgsConstructor
public class ThreadController {

    @GetMapping("/runnable")
    @ApiOperation(value = "实例化Runnable创建线程")
    public String thread() {
        Thread t1 = new Thread(() -> {
            log.info("实例化thread创建线程");
        }, "thread");
        t1.start();
        return "你好";
    }


    @GetMapping("/futureTask")
    @ApiOperation(value = "实例化futureTask创建线程")
    public String futureTask() throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(() -> {
            log.info("FutureTask");
            return "FutureTask";
        });
        Thread t1 = new Thread(task, "FutureTask");
        t1.start();
        return task.get();
    }


    private static volatile boolean status = true;

    @GetMapping("/joinRun1")
    @ApiOperation(value = "测试 join1 使线程一直运行")
    public void joinRun1() {
        AtomicInteger i = new AtomicInteger();
        Thread t1 = new Thread(() -> {
            while (status) {
                try {
                    i.getAndIncrement();
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("join1 在运行");
            }
        }, "join1");

        Thread t2 = new Thread(() -> {
            log.info("join2 运行");
            try {
                // 等待 join1 执行结束 再继续执行
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("join2 运行,join运行" + i.get() + "秒");
        }, "join2");

        t1.start();
        t2.start();
    }

    @GetMapping("/joinCease1")
    @ApiOperation(value = "测试 join1 使线程停止")
    public void joinCease1() {
        status = false;
    }

    @GetMapping("/daemon")
    @ApiOperation(value = "创建守护线程")
    public void daemon() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            boolean interrupted = false;
            while (!interrupted) {
                // 这里不能调用 sleep wait join 当前线程会处于等待或者超时等待状态，isInterrupted 会抛出异常 ，并设置 interrupted 为 false
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    // 重新设置线程中断状态
                    Thread.currentThread().interrupt();
                    throw new RuntimeException("中断异常");
                }
                interrupted = Thread.currentThread().isInterrupted();
                log.info("daemon 在运行,interrupted 状态为"+interrupted);
            }
        }, "daemon");
        // 设置为守护线程
        t1.setDaemon(true);
        t1.start();
        TimeUnit.SECONDS.sleep(10);
        // 中断线程 线程如果处于阻塞状态 会抛出一个 InterruptedException
        t1.interrupt();
        log.info("daemon interrupted 状态为"+t1.isInterrupted());
    }


}
